
<template>
  <div id="goodD" :class='{"isiphone":$isiphone}'>
    <div class="swiper" @click='pre'>
      <div @click.stop="linkTo" class="icon-area">
        <div class="icon"></div>
      </div>
      <swiper @on-index-change='indexChange' :list="swiperList" style="width:100%;margin:0 auto;" height="6.55rem" dots-class="custom-bottom" dots-position="center"></swiper>
    </div>
    <div class="top">
      <div class="one">
        <h3>{{name}}</h3>
        <p>
          <img src="../../../wechat/static/images/spxq_bq.png" alt="">
          <span>{{type}}</span>
        </p>
        <span>
          ￥{{money}}
        </span>
      </div>
      <div class="four">
        <div class="specs" v-if="type=='实体商品'">
          规格
          <span class='specsBtn' 
          :class="{'choose':specsIndex==index}" 
          v-for="(item, index) in specsList" 
          :key="index" 
          @click="chooseSpecs(item,index)">{{item.name}}</span>
        </div>
        <div class="specs" v-if="type=='实体商品'">
          运费
          <span class="freight" v-if="detail.freight">
            ￥{{detail.freight}};满{{detail.pinkageTerm}}件包邮
          </span>
          <span class="freight" v-else>包邮</span>
        </div>
        <!-- <div class="numberBox">
          数量
          <span class='btnBox'>
            <span class='reduce' @click='reduce'>
              -
            </span>
            <span class='number'>
              {{number}}
            </span>
            <span class="add" @click='add'>
              +
            </span>
          </span>
        </div> -->
      </div>
      <div class="two">
        <span class="span1">购买须知</span> <span class="span2">{{news}}</span>
      </div>
      <div class="three">
        <h3>
          <i></i>
          商品详情
        </h3>
        <div v-html="content"></div>
      </div>
      <div  v-if="detail.applyShopMsg" class="three">
        <h3>
          <i></i>
          适用门店信息
        </h3>
        <div v-html="detail.applyShopMsg"></div>
      </div>
      
    </div>
    <div class="bottom">
      <a href=javascript:; @click="buyNow">立即购买</a>
    </div>
    <div  @click="scrollTopBtn" class="top-icon">
      <img src="../../../wechat/static/images/top.png" alt="">
    </div>
  </div>
</template>

<script>
import { Swiper } from "vux";
export default {
  name: "goodD",
  data() {
    return {
      id: "",
      name: "",
      money: "",
      moneyX1: "",
      content: "",
      type: "",
      news: "",
      cIndex: 0,
      isOn: '',
      swiperList: [
      ],
      specsList:[],
      specsIndex:0,
      specsPrice:'',
      number:1,
      numberMax:0,
      detail:{},
      state:true
    };
  },
  mounted() {
    this.id = this.$route.query.id;
    this.getDetail()
    
  },
  created() {},
  methods: {
    add(){
      this.number=(this.number==this.numberMax?this.number:(this.number+1))
      this.money = this.number*this.moneyX1
      if(this.number==this.numberMax){
        this.$vux.toast.show({
          text: '该商品24个小时内只能购买3张!',
          type: "warn",
          width: "auto"
        });
      }
    },
    reduce(){
      this.number=(this.number>1?(this.number-1):1)
      this.money = this.number*this.moneyX1
    },
    chooseSpecs(item,index){
      this.moneyX1=this.specsPrice = parseFloat(item.price)
      this.specsIndex = index
      this.money = this.number*this.moneyX1
    },
    indexChange(index) {
      this.cIndex = index
    },
    pre() {
      wx.previewImage({
		  	current: this.swiperList[this.cIndex].img, // 当前显示图片的http链接
		  	urls: this.swiperList.map(item=>{
          return item.img
        }) // 需要预览的图片http链接列表
      })
    },
    buyNow(){
      // if(!this.state){
      //   this.$vux.toast.show({
      //       text: '购买次数不能超过三次',
      //       type: "warn",
      //       width: 'auto'
      //     });
      //     return
      // }
      localStorage.setItem('buy_number',this.number)
      if(this.type=='实体商品'){
        localStorage.setItem('buy_specs',this.specsList[this.specsIndex].price)
        localStorage.setItem('buy_model',this.specsList[this.specsIndex].name)
      }else{
        localStorage.setItem('buy_specs','')
        localStorage.setItem('buy_model','')
      }
      this.$router.push('./confirmOrder?id='+this.id)
    },
    scrollTopBtn(){
      document.body.scrollTop=document.documentElement.scrollTop=0
    },
    linkTo(){
      if(localStorage.getItem('userType') == 'special'){
        this.$router.push('/homeC')
      }else{
        this.$router.push('/home')
      }
      
    },
    getDetail() {
      let url =
        this.$CONSTANT.GOODS.findEsGoodsById +
        "?sessionId=" +
        this.$common.getCookie("sessionId");
      let param = {
        id: this.id
      };
      //this.$vux.loading.show({
      //   text: "loading"
      // });
      this.$common.post(url, param, res => {
        setTimeout(() => {
          this.$vux.loading.hide()
        }, 500);
        if (res.status == 200) {
          var bussData = res.data.bussData;
          this.detail = bussData
          this.name = bussData.goodsName
          this.type = bussData.type=='real'?'实体商品':bussData.type=='virtual'?'自营电子券':'第三方电子券'//real为实体商品，virtual为自营电子，third为第三方电子
          this.news = bussData.notice
          this.money =this.moneyX1 = bussData.price
          if(bussData.type=='real'&&bussData.modelPrice){
            let arr = bussData.modelPrice.split('::')
            this.specsList = []
            arr.map(item=>{
              let arr1 = item.split('&&')
              this.specsList.push({
                name:arr1[0],
                price:arr1[1]
              })
            })
            this.money =this.moneyX1 = this.specsList[0].price
          }
          if(bussData.type =='third'){
            this.getNum()
          }
          this.isOn = bussData.isOn
          this.content = bussData.details
          this.numberMax = bussData.restNum
          this.swiperList = bussData.photos.map(item=>{
            return {
                url: "",
                img: item.fileUrl,
                title: ""
            }
          })
        } else {
          this.$vux.toast.show({
              text: res.msg,
              type: "warn",
              width: 'auto'
            });
        }
      });
    },
    getNum() {
      let url =
        `${this.$CONSTANT.GOODS.payorderNum}?goodId=${this.id}&quotaNum=3&buyNum=1&sessionId=${
        this.$common.getCookie("sessionId")}`
      this.$common.post(url, {}, res => {
        let data = res.data.bussData
        this.state = data
      });
    }
  },
  components: {
    Swiper
  }
};
</script>

<style lang="less">
#goodD {
  // margin-bottom: 60px;
  padding-bottom: 55px !important;
  overflow-x: hidden;
  .top-icon{
    position: fixed;
    right: 10px;
    bottom: 100px;
    img{
      width:49px;
      height:49px;
      background:rgba(0,0,0,1);
      box-shadow:0px 6px 14px 2px rgba(104,104,104,0.1);
      opacity:0.54;
      border-radius:50%;
    }
  }
  .swiper{
    position: relative;
     .icon-area{
       width: 60px;
       height: 50px;
        position: absolute;
        right:0px;
        top: 9px;
        z-index: 1001;
     }
     .icon{
        background-image: url('../../../wechat/static/images/home.png');
        background-size: 100% 100%;
        height: 17.5px;
        width: 19px;
        margin: 10px auto;
      }
    .vux-img{
      background-size: contain;
    }
  }
  .top{
    .four{
      background: #fff;
      padding: 0 11px;
      margin-top: 10px;
      div.specs{
        min-height: 59px;
        line-height: 59px;
        font-family: PingFang-SC-Medium;
        font-size: 13px;
        color: #333;
        .freight{
          margin-left: 16px;
        }
        .specsBtn{
          display: inline-block;
          min-width: 15.7%;
          padding: 0 3px;
          // height: 29px;
          max-width: 100%;
          line-height: 29px;
          text-align: center;
          background: #fff;
          border: 1px solid #C2C2C2;
          border-radius: 2px;
          margin-right: 8px;
          color: #999999;
          &:first-child{
            margin-left: 17px;
          }
        }
        .choose{
          background: #EE3C4A;
          border-color: transparent;
          color: #fff;
        }
      }
      .numberBox{
        height: 59px;
        line-height: 59px;
        font-family: PingFang-SC-Medium;
        font-size: 13px;
        color: #333;
        .btnBox{
          float: right;
          font-size: 0;
          span{
            width: 30px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            display: inline-block;
            font-family: PingFang-SC-Medium;
            font-size: 14px;
            color: #666;
            margin-right: 3px;
          }
          .reduce{
            background: #F7F7F7;
          }
          .number{
            background: #EFEFEF;
          }
          .add{
            background: #EFEFEF;
          }
        }
      }
    }
    .one{
      background: #fff;
      padding: 16px 11px 25px;
      h3{
        font-family: PingFang-SC-Bold;font-weight: 600;
        font-size: 15px;
        color: #333;
      }
      p{
        margin: 21px 0;
        img{
          width: 11px;
          height: 11px;
          vertical-align: middle;
        }
        span{
          font-family: PingFang-SC-Medium;
          font-size: 13px;
          color: #999;
          margin-left: 7px;
        }
      }
      >span{
        font-family: PingFang-SC-Bold;font-weight: 600;
          font-size: 14px;
        color: #EA030B;
      }
    }
    .two{
      background: #fff;
      font-family: PingFang-SC-Medium;
      color: #999;
      min-height: 60px;
      margin: 8px 0;
      padding-left: 11px;
      font-size: 0px;
      padding-top: 12px;
      .span1{
        width: 20%;
        font-size: 13px;
        color: #999;
        word-break: break-word;
        line-height: 20px;
        display: inline-block;
        float: left;
      }
      span.span2{
        float: right;
        color: #333;
        font-size: 13px;
        word-break: break-word;
        line-height: 20px;
        display: inline-block;
        width: 80%;
        vertical-align: text-top;
      }
    }
    .three{
      background: #fff;
      h3{
        font-family: PingFang-SC-Bold;font-weight: 600;
        font-size: 14px;
        color: #333;
        padding-left: 10px;
        padding-top: 19px;
        i{
          display: inline-block;
          width: 3px;
          height: 13px;
          background: #FF4A68;
          border: 1px solid transparent;
          border-radius: 1px;
          margin-right: 7px;
        }
      }
      div{
        padding: 10px;
        img{
          max-width: 100%;
        }
      }
    }
  }
  .bottom{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    border-top: 1px solid #EDEDED;
    background: #fff;
    height: 55px;
    a{
      float: right;
      height: 38px;
      width: 110px;
      line-height: 38px;
      background: #EE3C4A;
      color: #fff;
      border: 1px solid transparent;
      border-radius: 19px;
      text-align: center;
      font-family: PingFang-SC-Bold;font-weight: 600;
      font-size: 13px;
      color: #fff;
      margin-top: 9px;
      margin-right: 10px;
    }
  }
}
</style>
